/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;
import com.fujitsu.nanjing.tieba.service.UserService;

/**
 * ForumController.java
 * 
 * @author Qixiaobo-win8
 */
@Controller
@RequestMapping("/myforum")
public class ForumController extends BaseController {
	@Resource
	private UserService userService;
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register(Model model) {
		return "/myforum/register";

	}
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String doRegister(Model model, CoreUser user, HttpSession session,
			String verifycode, HttpServletRequest request) {
		String code = (String) session
				.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if (code.equals(verifycode)) {
			user.setRemoteip(request.getRemoteAddr());
			user.setCreatetime(new Date());
			user.setPwd(encrypt(user.getPwd()));
			user = userService.insert(user);
			setLoginState(session, userService.getUserName(user.getUserid()),
					user.getUserid());
			return redirect("/myforum/perform/" + encrypt(user.getUserid()));

		}
		return "/myforum/register";

	}
	@RequestMapping(value = "/perform/{userid}", method = RequestMethod.GET)
	public String perform(Model model, @PathVariable String userid,
			HttpServletRequest request) {
		CoreUser user = userService.findById(decrypt(userid));
		user.setPwd(decrypt(user.getPwd()));
		model.addAttribute("user", user);
		// TODO;存放着上次访问的页面
		return "myforum/peform";
	}
	/**
	 * ajax校验用户名是否被占用
	 * */
	@RequestMapping(value = "/checkUserId", method = RequestMethod.GET)
	@ResponseBody
	public boolean checkUserId(Model model, String userid) {
		CoreUser user = userService.findById(userid);
		if (user != null) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * ajax校验邮箱是否被占用
	 * */
	@RequestMapping(value = "/checkEmail", method = RequestMethod.GET)
	@ResponseBody
	public boolean checkEmail(Model model, String email) {
		int count = userService.findByEmail(email);
		if (count != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * ajax校验验证码是否正确
	 * */
	@RequestMapping(value = "/checkVerifyCode", method = RequestMethod.GET)
	@ResponseBody
	public boolean checkVerifyCode(Model model, String verifycode,
			HttpSession session) {
		String code = (String) session
				.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if (!code.equals(verifycode)) {
			return false;
		} else {
			return true;
		}
	}
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "/myforum/login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String doLogin(Model model, String userid, String pwd,
			String cookietime, String fromPath, HttpServletRequest request,
			HttpServletResponse response) {
		CoreUser user = userService.findByIdAndPwd(userid, encrypt(pwd));
		if (user == null) {
			return "/myforum/login";
		}
		if (fromPath != null && !fromPath.trim().equals("")) {
			userService.updateWhileLogin(user, cookietime);
			return redirect(fromPath);
		}
		setCookie(cookietime, userid, request, response);
		return redirect("/");
	}

	@RequestMapping(value = "/logout/{userid}", method = RequestMethod.GET)
	public String logOut(Model model, @PathVariable String userid,
			HttpSession session) {
		setLogOutStatus(session);
		return redirect("/");
	}
	// 设置Cookie
	public void setCookie(String cookietime, String userid,
			HttpServletRequest request, HttpServletResponse response) {

		if (!cookietime.equals("0")) {
			setCookie(request, response, CURRENTUSER,
					userService.getUserName(userid),
					Integer.parseInt(cookietime));
			setCookie(request, response, USERID, userid,
					Integer.parseInt(cookietime));
		}
	}
}
